<?php

/*echo "post:";
var_export($_POST);
echo "file:";
var_export($_FILES);*/
/*输出$_POST和$_FILES
 post:array ( 'id' => 'WU_FILE_1',
    'name' => '表格填写模板.rar',
    'type' => 'application/octet-stream',
    'lastModifiedDate' => '2020/6/28 下午3:44:14',
    'size' => '28989591',
    'chunks' => '6',//把文件分成多少个上传
    'chunk' => '0', );//位置，上传第几个
file:array ( 'file' => array ( 'name' => '表格填写模板.rar',
    'type' => 'application/octet-stream',
    'tmp_name' => '/tmp/phppIc9SH',
    'error' => 0,
    'size' => 5242880, ), );
*/

session_start();

/*单进程   threads:1*/
$url=__DIR__.'/tmp/'.$_FILES['file']['name'].$_POST['chunk'];
$bool=move_uploaded_file(
    $_FILES['file']['tmp_name'],
    $url
);
if ($bool)
{
    if (empty($_SESSION['file']))
    {
        $_SESSION['file']=array();
    }
    $_SESSION['file']
    [$_FILES['file']['name']]
    [$_POST['chunk']]=$url;
    $name= $_SESSION['file'][$_FILES['file']['name']];
    if (count( $name)==$_POST['chunks'])
    {
        $str='';
        for ($i=0;$i<count($name);$i++)
        {
            $str.=file_get_contents($name[$i]);
        }
        file_put_contents(
            __DIR__.'/tmp/'.time().$_FILES['file']['name']
            ,$str
        );
        // unlink();
        unset($_SESSION['file'][$_FILES['file']['name']]);
        echo 'ok';
    }else{
        echo '已上传的文件数量：',
        count($name),'文件的总数量',$_POST['chunks'];
    }
}

/*多进程*/
/*if ($_POST['act']==1)
{
    $name=$_SESSION['file']
    [$_POST['name']];
    $str='';
    for ($i=0;$i<count($name);$i++)
    {
        $str.=file_get_contents($name[$i]);
    }
    file_put_contents(
        __DIR__.'/tmp/'.time().$_POST['name']
        ,$str
    );
    echo 'ok';
}else{
    $url=__DIR__.'/tmp/'.$_FILES['file']['name'].$_POST['chunk'];
    //move_uploaded_file()函数把上传的文件移动到新位置。
    $bool=move_uploaded_file(
        $_FILES['file']['tmp_name'],
        $url
    );
    $_SESSION['file']
    [$_FILES['file']['name']]
    [$_POST['chunk']]=$url;
}*/


/*$urlt=__DIR__.'/tmp/'.$_FILES['file']['name'].'-'.$_POST['chunk'].microtime();
$bool=move_uploaded_file(
    $_FILES['file']['tmp_name'],
    $urlt
);
if ($bool)
{
    $url='/test/tmp/1.txt';
    $f=fopen($url,'wb+');
    $fc=flock($f,LOCK_EX);
    if ($fc)
    {
        $_SESSION['tfile'][$_FILES['file']['name']][$_POST['chunk']]=$urlt;
    }else{
        echo 'error';
        exit;
    }
    flock($f,LOCK_UN);
    fclose($f);
    $count= count($_SESSION['tfile'][$_FILES['file']['name']]);
    if ($count==$_POST['chunks'])
    {
        // echo 'ok';
        $str='';
        for ($i=0;$i<$count;$i++)
        {
            $v=$_SESSION['tfile'][$_FILES['file']['name']][$i];
            $str.=file_get_contents($v);
        }
        file_put_contents(__DIR__.'/tmp/'.time().$_FILES['file']['name'],$str);
        for ($i=0;$i<$count;$i++)
        {
            $v=$_SESSION['tfile'][$_FILES['file']['name']][$i];
            unlink($v);
        }
        unset($_SESSION['tfile'][$_FILES['file']['name']]);
        echo 'ok';
    }else{
        echo 'no ok';
    }
    // echo 'ok';
}*/